/**********************************************************************************************************************
 * \file Cpu0_Main.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 *
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
 * derivative works of the Software, unless such copies or derivative works are solely in the form of
 * machine-executable object code generated by a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *********************************************************************************************************************/
 /*\title UART communication via ASCLIN module
 * \abstract An ASCLIN module configured for UART communication sends "Hello World!" and receives the string via the internal loopback.
 * \description The string "Hello World!" is sent and received via UART through one pin due to the internal loopback.
 *              The data can be visualized using an oscilloscope.
 *
 * \name ASCLIN_UART_1_KIT_TC397_TFT
 * \version V1.0.1
 * \board APPLICATION KIT TC3X7 V2.0, KIT_A2G_TC397_5V_TFT, TC39xXX_B-Step
 * \keywords ASC, ASCLIN, ASCLIN_UART_1, AURIX, Oscilloscope, UART, communication
 * \documents https://www.infineon.com/aurix-expert-training/Infineon-AURIX_ASCLIN_UART_1_KIT_TC397_TFT-TR-v01_00_01-EN.pdf
 * \documents https://www.infineon.com/aurix-expert-training/TC39B_iLLD_UM_1_0_1_12_1.chm
 * \lastUpdated 2020-12-18
 *********************************************************************************************************************/
#include <asm/process.h>
#include <photon/irqflags.h>
#include <photon/printk.h>
#include <autosar/internal.h>
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "Ifx_Console.h"
#include "Ifx_Ssw_CompilersGnuc.h"
#include "IfxPort.h"
#include <asm/mpu.h>
#include <asm/can.h>
#include <asm/led.h>
IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;

extern void  start_master(void);
extern union process_union master_idle_stack;

/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define DUMMY_ELEMENTS_NUM          96          /* Array length. Must be multiple of DPR_GRANULARITY                */
#define INDEX_FORTY_EIGHT           48
#define INDEX_FORTY_SEVEN           47

extern void left_on(void);

extern void left_off(void);

extern void right_on(void);
extern void right_off(void);
void core0_main(void)
{
    unsigned int addr = ((uintptr_t)(&master_idle_stack) + PROCESS_STACK_SIZE);
    Ifx_CPU_PSW PSWRegisterValue;

    IfxCpu_enableInterrupts();
    initLED();
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());

    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    __asm__ volatile (" mov.a\t %%a10, %0" : :"d" (addr):"memory");
#ifdef CONFIG_AUTOSAR_MEMORY_PROTECTION
    PSWRegisterValue.U = __mfcr(CPU_PSW);               /* Get the Program Status Word (PSW) register value         */
    PSWRegisterValue.B.PRS = PROTECTION_SET_1;             /* Set the PRS bitfield to enable the Protection Set        */
    __mtcr(CPU_PSW, PSWRegisterValue.U);                /* Set the Program Status Word (PSW) register               */
#endif
    start_master();
}
